package com.cdks.wuxiu.api.component.aliyun;

import java.time.LocalDate;
import java.util.TreeMap;

import com.cdks.wuxiu.api.util.HttpUtils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class RealNameAuth
{
    public static void init(String env)
    {
    }

    public static int checkInfo(String idCard, String phone, String name)
    {
        String url = "https://slytransf.market.alicloudapi.com/mobile_transfer";
        String appCode = "3c2f1bf7fb90456a90bd92733d50c67a";

        TreeMap<String, String> header = new TreeMap<>();
        header.put("Authorization", "APPCODE " + appCode);

        TreeMap<String, String> params = new TreeMap<>();
        params.put("idcard", idCard);
        params.put("name", name);
        params.put("mobile", phone);

        try {
            String ret  = HttpUtils.send_get_request(url, header, params);
            log.info(ret);
            JSONObject info  = JSONUtil.parseObj(ret);
            if (info.getInt("code") != 200)
            {
                return -2;
            }

            JSONObject data  = info.getJSONObject("data");
            if (data == null)
            {
                return -3;
            }
            
            String result  = data.getStr("result");
            if ((result == null) || (!result.equals("0")))
            {
                return -4;
            }

            String birthday  = data.getStr("birthday");
            if ((LocalDate.now().getYear() - Integer.parseInt(birthday.substring(0, 4))) < 22)
            {
                return -5;
            }

            if (data.getStr("sex").equals("男"))
            {
                return 1;
            }

            if (data.getStr("sex").equals("女"))
            {
                return 2;
            }
        } catch (Exception e) {
            log.error(e.getMessage());

            return -1;
        }

        return -1;
    }
}
